package com.modelclient.app.test;

import com.modelclient.baselib.config.Config;
import com.modelclient.baselib.http.RetrofitUtil;
import com.modelclient.baselib.model.CommonBaseResponse;
import com.modelclient.baselib.model.MineDataBean;
import com.modelclient.baselib.util.CookieNewUtil;
import com.modelclient.baselib.util.SPUtils;
import com.modelclient.baselib.util.ThrowableConsumer;
import com.modelclient.baselib.util.Utils;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;

/**
 * Created by TMVPHelper on 2017/07/18
 */
public class MinePresenter extends MineContract.Presenter {

    @Override
    public void getCustomerInfo() {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        builder.addFormDataPart("token", SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));
        builder.addFormDataPart("app_version", CookieNewUtil.getVersionName(Utils.getContext()));
        builder.addFormDataPart("platform","android");

        Observable<CommonBaseResponse<MineDataBean>> observable = RetrofitUtil.getInstance().getCustomerInfo(builder.build().parts());
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<MineDataBean>>) bean -> {
            if (bean != null && 0 == bean.getCode()) {
                mView.getCustomerInfoSuccess(bean.data);
            } else {
                mView.getCustomerInfoFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.getCustomerInfoFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void logout() {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        builder.addFormDataPart("token",SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));

        Observable<CommonBaseResponse> observable = RetrofitUtil.getInstance().logout(builder.build().parts());
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if (bean != null && 0 == bean.getCode()) {
                mView.logout();
            } else {
                mView.logoutFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.logoutFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void cancelSubscribe(String eventId) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        builder.addFormDataPart("event_id",eventId);
        builder.addFormDataPart("token", SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));

        Observable<CommonBaseResponse> observable = RetrofitUtil.getInstance().cancelSubscribe(builder.build().parts());
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if (bean != null && 0 == bean.getCode()) {
                mView.cancelSuccess();
            } else {
                mView.cancelFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.cancelFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}